/**
 * Created by gengrong on 16/4/14.
 */
import { Form, Input, Modal } from 'antd';
import * as React from 'react';

class UserResetPasswordModalForm extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      createState: {}
    };
  }

  render() {
    const passwordProps = this.props.form.getFieldProps('password', {
      rules: [
        { required: true, min: 8, message: '请填写用户重置后密码, 至少为8位' }
      ]
    });
    return (
      <Form horizontal form={this.props.form}>
        <Form.Item labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label="重置密码：">
          <Input {...passwordProps} placeholder="输入用户重置后密码"/>
        </Form.Item>
      </Form>
    )
  }
}

UserResetPasswordModalForm = Form.create({})(UserResetPasswordModalForm);

class UserResetPasswordModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = { visible: false }
  }

  showModal(user = {}) {
    this.setState({
      visible: true,
      user: user
    });
    if (this.refs.form)
      this.refs.form.resetFields()
  }

  handleOk() {
    const {form} = this.refs;
    const {user = {}} = this.state
    const me = this;
    form.validateFieldsAndScroll((errors, values) => {
      if (!!errors) {
        return;
      }

      me.setState({
        confirmLoading: true
      });

      me.props.onResetPassword(user.id, values.password)
        .then(()=> {
          this.hideModel()
        });
    })
  }

  hideModel() {
    this.setState({
      visible: false,
      confirmLoading: false
    });
  }

  render() {
    return (
      <Modal title="重置密码"
        visible={this.state.visible}
        onOk={::this.handleOk}
        confirmLoading={this.state.confirmLoading}
        onCancel={::this.hideModel}>
        <UserResetPasswordModalForm ref="form"/>
      </Modal>
    );
  }
}

export default UserResetPasswordModal;
